#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import datetime
import json
from typing import Iterable, Any

import scrapy
from scrapy import Request
from scrapy.http import Response

from apps.tax_policy_declaration.tax_policy_declaration.items import NetTaxPolicyDeclarationItem
from utils.tools import to_date, urlencode, parse_url_params


class BjBjrmzfTaxPolicyDeclarationSpider(scrapy.Spider):
    name = "bj_bjrmzf_tax_policy_declaration"
    source = "北京市人民政府"
    province = "北京市"
    industry_list = [
        {
            "id": "A",
            "type": "industry",
            "key": "A",
            "value": "科技创新",
            "remark": None,
            "sort": None,
            "name": "科技创新",
        },
        {
            "id": "B",
            "type": "industry",
            "key": "B",
            "value": "商务贸易",
            "remark": None,
            "sort": None,
            "name": "商务贸易",
        },
        {"id": "C", "type": "industry", "key": "C", "value": "文化", "remark": None, "sort": None, "name": "文化"},
        {
            "id": "D",
            "type": "industry",
            "key": "D",
            "value": "软件和信息",
            "remark": None,
            "sort": None,
            "name": "软件和信息",
        },
        {"id": "E", "type": "industry", "key": "E", "value": "金融", "remark": None, "sort": None, "name": "金融"},
        {
            "id": "F",
            "type": "industry",
            "key": "F",
            "value": "产业互联网",
            "remark": None,
            "sort": None,
            "name": "产业互联网",
        },
        {"id": "G", "type": "industry", "key": "G", "value": "高精尖", "remark": None, "sort": None, "name": "高精尖"},
        {
            "id": "H",
            "type": "industry",
            "key": "H",
            "value": "知识产权",
            "remark": None,
            "sort": None,
            "name": "知识产权",
        },
        {
            "id": "I",
            "type": "industry",
            "key": "I",
            "value": "人力资源",
            "remark": None,
            "sort": None,
            "name": "人力资源",
        },
        {
            "id": "J",
            "type": "industry",
            "key": "J",
            "value": "农业农村",
            "remark": None,
            "sort": None,
            "name": "农业农村",
        },
        {
            "id": "K",
            "type": "industry",
            "key": "K",
            "value": "社会保障",
            "remark": None,
            "sort": None,
            "name": "社会保障",
        },
        {
            "id": "L",
            "type": "industry",
            "key": "L",
            "value": "节能环保",
            "remark": None,
            "sort": None,
            "name": "节能环保",
        },
        {
            "id": "M",
            "type": "industry",
            "key": "M",
            "value": "智能装备 ",
            "remark": None,
            "sort": None,
            "name": "智能装备 ",
        },
        {"id": "N", "type": "industry", "key": "N", "value": "旅游", "remark": None, "sort": None, "name": "旅游"},
        {
            "id": "O",
            "type": "industry",
            "key": "O",
            "value": "工程建设",
            "remark": None,
            "sort": None,
            "name": "工程建设",
        },
        {"id": "P", "type": "industry", "key": "P", "value": "标准化", "remark": None, "sort": None, "name": "标准化"},
        {
            "id": "Q",
            "type": "industry",
            "key": "Q",
            "value": "生物医药 ",
            "remark": None,
            "sort": None,
            "name": "生物医药 ",
        },
        {
            "id": "R",
            "type": "industry",
            "key": "R",
            "value": "创业孵化",
            "remark": None,
            "sort": None,
            "name": "创业孵化",
        },
        {"id": "S", "type": "industry", "key": "S", "value": "新能源", "remark": None, "sort": None, "name": "新能源"},
        {
            "id": "T",
            "type": "industry",
            "key": "T",
            "value": "先进制造",
            "remark": None,
            "sort": None,
            "name": "先进制造",
        },
        {
            "id": "U",
            "type": "industry",
            "key": "U",
            "value": "交通运输",
            "remark": None,
            "sort": None,
            "name": "交通运输",
        },
        {"id": "V", "type": "industry", "key": "V", "value": "新材料", "remark": None, "sort": None, "name": "新材料"},
        {"id": "W", "type": "industry", "key": "W", "value": "体育", "remark": None, "sort": None, "name": "体育"},
        {
            "id": "X",
            "type": "industry",
            "key": "X",
            "value": "专精特新",
            "remark": None,
            "sort": None,
            "name": "专精特新",
        },
        {"id": "Y", "type": "industry", "key": "Y", "value": "文物", "remark": None, "sort": None, "name": "文物"},
    ]
    regions = [
        {"type": "region", "name": "北京市", "count": 1, "value": "11", "sort": 0},
        {"type": "region", "name": "东城区", "count": 1, "value": "110101", "sort": 0},
        {"type": "region", "name": "西城区", "count": 1, "value": "110102", "sort": 0},
        {"type": "region", "name": "朝阳区", "count": 1, "value": "110105", "sort": 0},
        {"type": "region", "name": "丰台区", "count": 1, "value": "110106", "sort": 0},
        {"type": "region", "name": "石景山区", "count": 1, "value": "110107", "sort": 0},
        {"type": "region", "name": "海淀区", "count": 1, "value": "110108", "sort": 0},
        {"type": "region", "name": "门头沟区", "count": 1, "value": "110109", "sort": 0},
        {"type": "region", "name": "房山区", "count": 1, "value": "110111", "sort": 0},
        {"type": "region", "name": "通州区", "count": 1, "value": "110112", "sort": 0},
        {"type": "region", "name": "顺义区", "count": 1, "value": "110113", "sort": 0},
        {"type": "region", "name": "昌平区", "count": 1, "value": "110114", "sort": 0},
        {"type": "region", "name": "大兴区", "count": 1, "value": "110115", "sort": 0},
        {"type": "region", "name": "怀柔区", "count": 1, "value": "110116", "sort": 0},
        {"type": "region", "name": "平谷区", "count": 1, "value": "110117", "sort": 0},
        {"type": "region", "name": "密云区", "count": 1, "value": "110118", "sort": 0},
        {"type": "region", "name": "延庆区", "count": 1, "value": "110119", "sort": 0},
        {"type": "region", "name": "北京经济技术开发区", "count": 1, "value": "110120", "sort": 0},
    ]

    def start_requests(self) -> Iterable[Request]:
        for industry in self.industry_list:
            for region in self.regions:
                url = "https://zhengce.beijing.gov.cn/lron/v2/bones/th/search/unifySearch"
                params = {
                    "sort": "111",
                    "industry": industry["key"],
                    "regions": region["value"],
                    "page": "1",
                    "pageSize": "12",
                    "total": "0",
                    "enabled": "Y",
                    "type": "themeType",
                    "pageType": "themeType",
                    "requestStatus": "1",
                    "ignoreError": "Y",
                }
                yield Request(
                    url=url + "?" + urlencode(params),
                    callback=self.parse_list,
                    cb_kwargs={"industry": industry, "region": region},
                )
                params2 = {
                    "sort": "111",
                    "industry": industry["key"],
                    "regions": region["value"],
                    "page": "1",
                    "pageSize": "12",
                    "total": "0",
                    "enabled": "Y",
                    "type": "themeType",
                    "pageType": "themeType",
                    "requestStatus": "3",
                    "ignoreError": "Y",
                }
                yield Request(
                    url=url + "?" + urlencode(params2),
                    callback=self.parse_list,
                    cb_kwargs={"industry": industry, "region": region},
                )

    def parse_list(self, response, **kwargs):
        yield from self.parse(response, **kwargs)
        root_url, params = parse_url_params(response.request.url)
        resp = response.json()
        total = resp["data"]["pagination"]["totalPage"]
        if int(params["page"]) < int(total):
            yield Request(
                url=root_url + "?" + urlencode({**params, "page": int(params["page"]) + 1}),
                callback=self.parse_list,
                cb_kwargs=kwargs,
            )

    def parse(self, response: Response, **kwargs: Any) -> Any:
        resp = response.json()
        records = resp["data"]["list"]
        region = kwargs.get("region").get("name")
        level = "市级" if region == "北京市" else "区级"
        industry_mapping = {i["key"]: i["value"] for i in self.industry_list}
        for record in records:
            application_deadline = record.get("endDate")
            theme_type_id = (record.get("themeType") or {}).get("id")
            created_at = (record.get("themeType") or {}).get("createdAt")
            release_time = datetime.datetime.fromtimestamp(int(created_at) / 1000).strftime("%Y-%m-%d")
            valid = 1
            if application_deadline:
                if to_date(application_deadline) < datetime.datetime.now():
                    valid = 0
            item = NetTaxPolicyDeclarationItem(
                **{
                    "source": self.source,
                    "province": self.province,
                    "city": "北京市",
                    "county": None if region == "北京市" else region,
                    "declaration_type": None,
                    "page_view": record.get("visitCount"),
                    "project_name": record.get("title"),
                    "enjoyment_subject": None,
                    "enjoyment_condition": None,  #
                    "doc_number": None,
                    "release_time": release_time,
                    "department": record.get("govName"),
                    "policy_theme": "|".join([industry_mapping.get(i) for i in (record.get("industry") or [])]),
                    "policy_basis": None,
                    "policy_sunnary": None,
                    "max_support_amount": None,
                    "content": None,
                    "source_url": f"https://zhengce.beijing.gov.cn/#/themeTypeDetail?themeTypeId={theme_type_id}",
                    "application_area": None,
                    "administrative_level": level,
                    "policy_industry": None,
                    "declaration_start_time": record.get("startDate"),
                    "application_deadline": record.get("endDate"),
                    "application_materials": None,  #
                    "handling_process": None,  #
                    "charging_situation": None,
                    "processing_time": record.get("startDate"),
                    "legal_time_limit": None,
                    "commitment_time_limit": None,
                    "declaration_address": None,
                    "consultant": None,
                    "hotline": None,
                    "complaints_hotline": None,
                    "valid": valid,
                }
            )
            url = f"https://zhengce.beijing.gov.cn/lron/v2/bones/th/theme/search?requestStatus=0&themeTypeId={theme_type_id}&enabled=Y&sort=21&pageSize=100&ignoreError=Y"
            yield Request(
                url=url,
                method="GET",
                callback=self.parse_search,
                meta={"item": item},
            )

    def parse_search(self, response, **kwargs):
        item = response.meta["item"]
        resp = response.json()
        res = resp["data"]["list"]
        if res:
            record = res[0]
            record_id = record.get("id")
            url = f"https://zhengce.beijing.gov.cn/lron/v2/bones/th/theme/info?id={record_id}&_progress=true&ignoreError=Y"
            yield Request(
                url=url,
                method="GET",
                callback=self.parse_detail,
                meta={"item": item, "id": record_id},
            )

    def parse_detail(self, response, **kwargs):
        item = response.meta["item"]
        record_id = response.meta["id"]
        resp = response.json()
        if resp["status"] == 200:
            attachment_configs = resp["data"]["attachmentConfigs"]
            contact_phone = resp["data"]["contactPhone"]
            toward = resp["data"]["toward"]
            moneyMax = resp["data"]["moneyMax"]
            condition = resp["data"]["extensions"]["condition"]
            if attachment_configs:
                item["application_materials"] = ";".join([i["title"] for i in attachment_configs])
            if condition:
                item["enjoyment_condition"] = condition
                item["policy_sunnary"] = resp["data"]["extensions"]["description"]
            item["hotline"] = contact_phone
            item["max_support_amount"] = moneyMax
            item["enjoyment_subject"] = {1: "法人", 2: "个人", 3: "法人和个人"}.get(toward)
        url = f"https://zhengce.beijing.gov.cn/bs/api/v2/server/th/transactFlow/list?themeId={record_id}&page=1&pageSize=1000&terminalType=1"
        yield Request(
            url=url,
            method="GET",
            callback=self.parse_process,
            meta={"item": item, "id": record_id},
        )

    def parse_process(self, response, **kwargs):
        item = response.meta["item"]
        resp = response.json()
        record_id = response.meta["id"]
        if resp["status"] == 200:
            process_list = resp["data"]["list"]
            if process_list:
                item["handling_process"] = ";".join([f"{i['title']}:{i['content']}" for i in process_list])
        url = f"https://zhengce.beijing.gov.cn/lron/v2/bones/th/theme/relevantPolicies?id={record_id}&enabled=Y&pageSize=100&ignoreError=Y"
        yield Request(
            url=url,
            method="GET",
            callback=self.parse_rele,
            meta={"item": item},
        )

    def parse_rele(self, response, **kwargs):
        item = response.meta["item"]
        resp = response.json()
        if resp["status"] == 200:
            process_list = resp["data"]["list"]
            if process_list:
                item["policy_basis"] = "|".join([f"{i['title']}" for i in process_list])
        yield item


if __name__ == "__main__":
    from scrapy import cmdline

    cmdline.execute("scrapy crawl bj_bjrmzf_tax_policy_declaration".split())
